package com.china08.yxyapi.api.manage.sch.platform.website.image;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.sws.SwsAlbums;
import com.china08.yxyapi.entity.mg.sws.SwsAlbumsImage;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.repository.mg.SwsAlbumsImageRepository;
import com.china08.yxyapi.repository.mg.YxySwsAlbumsRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class SwsAlbumsDelImgController extends YxyApiBaseController {

    @Autowired
    SwsAlbumsImageRepository swsAlbumsImageRepository;

    @Autowired
    YxySwsAlbumsRepository yxySwsAlbumsRepository;

    @ApiOperation("删除相册图片")
    @RequestMapping(value = "/api_yw/manage/sch/platform/website/swsalbums/del/image", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {

        String id = reqModel.id;

        SwsAlbumsImage swsAlbumsImage = swsAlbumsImageRepository.findById(id);
        Assert.isTrue(null != swsAlbumsImage, "未获取到要删除的图片");

        swsAlbumsImageRepository.delete(swsAlbumsImage);

        // 修改相册图片总数量
        String albumId = swsAlbumsImage.getAlbumId();
        SwsAlbums swsAlbums = yxySwsAlbumsRepository.findById(albumId);
        // 获取相册图片总数量
        Integer countInteger = swsAlbumsImageRepository.countByAlbumId(albumId);
        swsAlbums.setImageCount(countInteger);
        yxySwsAlbumsRepository.save(swsAlbums);
    }

    protected static class ReqModel extends YxyApiPageReqModel {
        public String id;

        @Override
        public void autoCheck() {
            Assert.isTrue(StringUtils.isNotBlank(id), "未获取到要删除的图片");
        }
    }
}
